package com.dg.river.module.common.utils;

import com.dg.river.core.http.ApiManager;
import com.dg.river.core.http.BeanCode;
import com.dg.river.core.http.BeanUtils;
import com.dg.river.core.http.OkGoUtils;
import com.dg.river.core.http.OnResultListener;
import com.dg.river.core.http.RequestCodeEnum;
import com.dg.river.core.manager.UserInfoManager;
import com.dg.river.core.manager.UserInfoMethod;
import com.dg.river.core.util.TextUtil;
import com.dg.river.module.common.bean.NormalBean;
import com.dg.river.module.main.bean.UserInfoBean;

import java.util.HashMap;
import java.util.Map;

/**
 * @作者：Hututu
 * @时间：2023/4/3 13:54
 * @描述：内容描述
 * @修改：修改内容
 * @Version:1.0
 */
public class UserUtils {

    private static UserUtils userUtils;

    public static UserUtils getInstance() {
        if (userUtils == null) {
            synchronized (UserUtils.class) {
                if (userUtils == null) {
                    userUtils = new UserUtils();
                }
            }
        }
        return userUtils;
    }

    public interface UserResultListener {

        void onResult(UserInfoBean.DataDTO dataDTO);

        void onError(String msg);
    }

    /**
     * 获取用户信息
     */
    public void initUserInfo(UserResultListener userResultListener) {
        OkGoUtils.getInstance()
                .url(ApiManager.User.GET_USER_INFO)
                .tag("获取用户信息")
                .userToken()
                .executeGet(new OnResultListener() {
                    @Override
                    public void onSuccess(String json) {
                        UserInfoBean userInfoBean = BeanUtils.getInstance().convertBean(json, UserInfoBean.class, BeanCode.USER_INFO);
                        if (BeanUtils.getInstance().checkSuccess(userInfoBean)) {
                            String phone = TextUtil.isEmptyConvert(userInfoBean.getData().getPhone());
                            String nickName = TextUtil.isEmptyConvert(userInfoBean.getData().getNickName());
                            String headAvatar = TextUtil.isEmptyConvert(userInfoBean.getData().getHeadPicture());
                            String shipName = TextUtil.isEmptyConvert(userInfoBean.getData().getShipName());
                            String account = TextUtil.isEmptyConvert(userInfoBean.getData().getAccount());
                            // 保存进SP
                            UserInfoManager.getInstance().userPhone(UserInfoMethod.SET, TextUtil.isEmptyConvert(phone));
                            UserInfoManager.getInstance().userNickName(UserInfoMethod.SET, TextUtil.isEmptyConvert(nickName));
                            UserInfoManager.getInstance().userAvatar(UserInfoMethod.SET, TextUtil.isEmptyConvert(headAvatar));
                            UserInfoManager.getInstance().userShipName(UserInfoMethod.SET, TextUtil.isEmptyConvert(shipName));
                            UserInfoManager.getInstance().userAccount(UserInfoMethod.SET, TextUtil.isEmptyConvert(account));
                            // 返回信息
                            if (userResultListener != null) {
                                userResultListener.onResult(userInfoBean.getData());
                            }
                        } else {
                            if (userResultListener != null) {
                                userResultListener.onError(BeanUtils.getInstance().toastMsg(userInfoBean));
                            }
                        }
                    }

                    @Override
                    public void onError(RequestCodeEnum requestCodeEnum, String errorMsg) {
                        super.onError(requestCodeEnum, errorMsg);
                        if (userResultListener != null) {
                            userResultListener.onError(errorMsg);
                        }
                    }
                });
    }

    public interface UserUpdateResultListener {

        void onResult(NormalBean normalBean);

        void onError(String msg);
    }

    /**
     * 修改用户信息
     */
    public void updateUser(String nickName, String headPicture, UserUpdateResultListener userUpdateResultListener) {
        Map<String, Object> map = new HashMap<>();
        if (!TextUtil.isEmpty(nickName)) {
            map.put("nickname", nickName);
        }
        if (!TextUtil.isEmpty(headPicture)) {
            map.put("headPicture", headPicture);
        }
        OkGoUtils.getInstance()
                .url(ApiManager.User.POST_UPDATE_USER)
                .tag("修改用户信息")
                .userToken()
                .paramsToJsonMap(map)
                .executePost(new OnResultListener() {
                    @Override
                    public void onSuccess(String json) {
                        NormalBean normalBean = BeanUtils.getInstance().convertBean(json, NormalBean.class, BeanCode.NORMAL);
                        if (BeanUtils.getInstance().checkSuccess(normalBean)) {
                            userUpdateResultListener.onResult(normalBean);
                        } else {
                            userUpdateResultListener.onError(BeanUtils.getInstance().toastMsg(normalBean));
                        }
                    }

                    @Override
                    public void onError(RequestCodeEnum requestCodeEnum, String errorMsg) {
                        super.onError(requestCodeEnum, errorMsg);
                        userUpdateResultListener.onError(errorMsg);
                    }
                });
    }

    /**
     * 修改用户手机号
     *
     * @param userPhone 手机号
     * @param smsCode   验证码
     */
    public void updateUserPhone(String userPhone, String smsCode, UserUpdateResultListener userUpdateResultListener) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", userPhone);
        map.put("code", smsCode);
        OkGoUtils.getInstance()
                .url(ApiManager.User.POST_UPDATE_PHONE)
                .tag("修改用户手机号")
                .userToken()
                .paramsMap(map)
                .executePost(new OnResultListener() {
                    @Override
                    public void onSuccess(String json) {
                        NormalBean normalBean = BeanUtils.getInstance().convertBean(json, NormalBean.class, BeanCode.NORMAL);
                        if (BeanUtils.getInstance().checkSuccess(normalBean)) {
                            userUpdateResultListener.onResult(normalBean);
                        } else {
                            userUpdateResultListener.onError(BeanUtils.getInstance().toastMsg(normalBean));
                        }
                    }

                    @Override
                    public void onError(RequestCodeEnum requestCodeEnum, String errorMsg) {
                        super.onError(requestCodeEnum, errorMsg);
                        userUpdateResultListener.onError(errorMsg);
                    }
                });
    }
}
